package ch.elektroplan.cmueller.logikModulGenerator.presentation.subs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ch.elektroplan.cmueller.logikModulGenerator.business.elements.Input;

public class InputPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	JLabel lb_name = new JLabel("Name");
	JLabel lb_initialisatinValue = new JLabel("Iniltialisationvalue");
	JLabel lb_dataformat = new JLabel("Dataformat");
	JLabel lb_comment = new JLabel("Kommentar");
	
	JTextField tf_name = new JTextField();
	JTextField tf_comment = new JTextField();
	JTextField tf_initialisationvalue = new JTextField();
	
	String[] dataforamt = {"nummerisch", "text"};
	JComboBox cb_dataformat = new JComboBox(dataforamt);
	
	Input input;

	public InputPanel(Input input) {
		
		this.input = input;
		
		
		setPreferredSize(new Dimension(400, 50));
		setLayout(new GridLayout(2, 4));
		
		add(lb_name);
		add(lb_dataformat);
		add(lb_initialisatinValue);
		add(lb_comment);
		
		this.tf_comment.addFocusListener(new LostFocusListener(tf_comment));
		this.tf_initialisationvalue.addFocusListener(new LostFocusListener(tf_initialisationvalue));
		this.tf_name.addFocusListener(new LostFocusListener(tf_name));
		this.cb_dataformat.addFocusListener(new LostFocusListener(cb_dataformat));

		add(tf_name);
		add(cb_dataformat);
		add(tf_initialisationvalue);
		add(tf_comment);			
		
		setVisible(true);
	}
	
	private class LostFocusListener implements FocusListener {

		private JComponent jComponent;

		public LostFocusListener(JComponent jComponent) {
			this.jComponent = jComponent;
		}

		@Override
		public void focusGained(FocusEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void focusLost(FocusEvent e) {

			if (jComponent.equals(tf_name)) {
				JTextField tf = (JTextField) jComponent;
				input.setName(tf.getText());
			}
			if (jComponent.equals(tf_initialisationvalue)) {
				JTextField tf = (JTextField) jComponent;
				input.setInitialisationValue(tf.getText());
			}
			if (jComponent.equals(tf_comment)) {
				JTextField tf = (JTextField) jComponent;
				input.setComment(tf.getText());
			}
			if (jComponent.equals(cb_dataformat)) {
				JComboBox cb = (JComboBox) jComponent;
				input.setDataformat(cb.getSelectedIndex());
			}

		}

	}

}
